<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","survey_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_survey_assignment.php";
$FileTemplate  = "ais_transaction_survey_assignment_list.html";
$TableName     = "tblsurveyassgn";
$SubHeaderTitle= "Survey Assignment List";
$PageSize      = 10 ;
$HTMLGridList  = "SurveyGrid";
$created_by     = $_SESSION['AISUser'];
//************ FORM ACTION **************
	if (GetParam("Action","")=="Delete") {
		DeleteRecord ("txtCheck",$PageSize);
	}

//Initiate Database Table For Search;
$survey_code = GetParam("survey_code","");
$projName = GetParam("projName","");
$custName = GetParam("custName","");


//******* DATABASE FORM QUERY ***********


    $SQL = "SELECT a.*,b.*,c.*,d.*,
			if(a.survey_appv_status = 'draft', 'Draft', if(a.survey_appv_status = 'wait_approved', 'Wait for Approval', if(a.survey_appv_status = 'approved', 'Approved', 'No Approval Status'))) as approval_status 
			FROM `$TableName` as a ";
	$SQL.= "JOIN tblproject as b ON a.project_id = b.project_id "; 
	$SQL.= "JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id ";
	$SQL.= "JOIN tbldebtur as d ON a.debtur = d.CUCODE ";

	
	if ($projName) 
	{
		if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$projName."%'";
		else $WHERE .= " b.project_name like '%".$projName."%'";
	}
	
	if($custName)
    {
		if (strlen($WHERE)) $WHERE .= " AND "." d.CUNAME like '%".$custName."%'";
		else $WHERE .= " d.CUNAME like '%".$custName."%'";
	}
	
	if (strlen($WHERE))	$WHERE = " WHERE ".$WHERE;
		else $WHERE = " WHERE 1 ";

	$SQL = $SQL.$WHERE." ORDER BY survey_id DESC";
	
	
	
	
	$WHERE = "";
	
	$SQLCount = "SELECT count(*) FROM `$TableName` as a ";
	$SQLCount.= "JOIN tblproject as b ON a.project_id = b.project_id "; 
	$SQLCount.= "JOIN ".$DBGeneral.".tblstaff as c ON a.staff_id = c.staff_id ";
	$SQLCount.= "JOIN tbldebtur as d ON a.debtur = d.CUCODE ";
	
	
	if ($projName) 
	{
		if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$projName."%'";
		else $WHERE .= " b.project_name like '%".$projName."%'";
	}
	
	if($custName)
    {
		if (strlen($WHERE)) $WHERE .= " AND "." d.CUNAME like '%".$custName."%'";
		else $WHERE .= " d.CUNAME like '%".$custName."%'";
	}
		
	if (strlen($WHERE))	$WHERE = " WHERE ".$WHERE;
		else $WHERE = " WHERE 1 ";

	$SQLCount = $SQLCount.$WHERE." ORDER BY survey_id DESC";
	
	

//Select Department 
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ; 

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->Delete("$TableName",$sequence_name.$x);
	    }
	}
	header("Location: $FileName");
	exit;	
	
}

?>
